/**
 * 版权所有(C)，上海海鼎信息工程股份有限公司，2007，所有权利保留。
 * 
 * 项目名：	rumba
 * 文件名：	ActionDBAppender.java
 * 模块说明：	
 * 修改历史：
 * Apr 16, 2007 - lxm - 创建。
 */
package com.hd123.rumba.log;

import java.util.Date;
import java.util.HashMap;

import org.apache.log4j.AppenderSkeleton;
import org.apache.log4j.spi.LoggingEvent;

import com.hd123.rumba.domain.IDomain;
import com.hd123.rumba.domain.IDomainMgr;
import com.hd123.rumba.exception.BusinessException;
import com.hd123.rumba.runtime.NamingMgr;

/**
 * @author lxm
 * 
 */
public class ActionDBAppender extends AppenderSkeleton {

  /** 消息前缀 */
  public static final String PREFIX_MESSAGE = "_ActionLog_";
  /** 键名：Action标题 */
  public static final String KEY_ACTION_CAPTION = "caption";
  /** 键名：所属域uuid */
  public static final String KEY_DOMAIN_UUID = "domain";
  /** 键名：操作人 */
  public static final String KEY_OPERATOR = "operator";
  /** 键名：结转期号 */
  public static final String KEY_SETTLENO = "settleNo";
  /** 键名：事件 */
  public static final String KEY_EVENT = "event";
  /** 键名：消息 */
  public static final String KEY_MESSAGE = "message";

  @Override
  protected void append(LoggingEvent event) {
    HashMap msgs = decodeMessage(event.getMessage().toString());
    if (!msgs.containsKey(PREFIX_MESSAGE))
      return;

    try {
      writeToDB(msgs, event);
    } catch (Exception e) {
      this.errorHandler.error("将日志信息写入数据库时发生错误。", e, -1, event);
    }
  }

  @Override
  public void close() {
    if (this.closed)
      return;
    this.closed = true;
  }

  @Override
  public boolean requiresLayout() {
    return false;
  }

  private IActionLogMgr logMgr = null;
  private IDomainMgr domainMgr = null;

  private IActionLogMgr getLogMgr() throws BusinessException {
    if (logMgr == null)
      logMgr = (IActionLogMgr) NamingMgr.getInstance().lookupMgr(IActionLogMgr.class);
    return logMgr;
  }
  
  private IDomainMgr getDomainMgr() throws BusinessException {
    if (domainMgr == null)
      domainMgr = (IDomainMgr) NamingMgr.getInstance().lookupMgr(IDomainMgr.class);
    return domainMgr;
  }

  private HashMap decodeMessage(String message) {
    HashMap map = new HashMap();
    String[] msgs = message.split("#");
    for (int i = 0; i < msgs.length; i++) {
      String[] kv = msgs[i].split("=", 2);
      String key = kv[0];
      String value = kv.length > 1 ? kv[1] : null;
      map.put(key, value);
    }
    return map;
  }

  private void writeToDB(HashMap msgs, LoggingEvent event) throws Exception {
    IActionLog log = getLogMgr().createLog();
    
    log.setActionClass(event.getLoggerName());
    log.setActionCaption((String) msgs.get(KEY_ACTION_CAPTION));
    
    String domainUuid = (String) msgs.get(KEY_DOMAIN_UUID);
    IDomain domain = getDomainMgr().getDomain(domainUuid);
    log.setDomain(domain);
    
    log.setOperator((String) msgs.get(KEY_OPERATOR));
    log.setTime(new Date(event.timeStamp));
    log.setSettleNo((String) msgs.get(KEY_SETTLENO));
    log.setEvent((String) msgs.get(KEY_EVENT));
    log.setMessage((String) msgs.get(KEY_MESSAGE));
    
    getLogMgr().saveT(log);
  }
}
